package com.xfp.service.impl;

import com.github.pagehelper.PageHelper;
import com.xfp.mapper.ChannelMapper;
import com.xfp.mapper.VmMapper;
import com.xfp.pojo.dto.ChannelConfigDto;
import com.xfp.pojo.entity.Channel;
import com.xfp.pojo.entity.Goods;
import com.xfp.pojo.vo.ChannelVo;
import com.xfp.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private VmMapper vmMapper;

    /**
     * 获取售货机活到详情
     * @param innerCode
     * @return
     */
    @Override
    public List<Channel> channelList(String innerCode) {
        List<Channel> list =  channelMapper.channelList(innerCode);
        list.forEach(l->{
            Goods goods = channelMapper.findGoodsById(l.getSkuId());
            l.setSku(goods);
        });

        return list;
    }

    /**
     * 货道配置
     * @param dto
     */
    @Override
    public void config(ChannelConfigDto dto) {
        log.info("货道配置：{}",dto.getChannelList().size());
        //补全数据并新增
        Integer maxCapacity = vmMapper.findMaxCapacityIdByVmInnerCode(dto.getInnerCode());
        Integer vmId = vmMapper.findVmIdByVmInnerCode(dto.getInnerCode());
        dto.setVmId(vmId);
        dto.setMaxCapacity(maxCapacity);
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        //删除原数据
        channelMapper.deleteChannel(dto.getInnerCode());
        channelMapper.insertChannel(dto);
    }
}
